<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Xy\Rbac\Middleware\AccessAuthenticateMiddleware;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();
    
        //后台
        $this->mapAdminRoutes();
    
        //微信第三方平台
        $this->mapTppV1Routes();
    
        //开放应用管理接口
        $this->mapApplicationRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::group([
            'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require base_path('routes/web.php');
        });
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            'prefix' => 'api',
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }
    
    /**
     * 后台路由
     */
    protected function mapAdminRoutes() {
        /** @var \Dingo\Api\Routing\Router $api */
        $api = app(\Dingo\Api\Routing\Router::class);
        
        // api接口
        $api->version(['v1'], function (\Dingo\Api\Routing\Router $api) {
            // 以下路由需求授权
            $api->group(['prefix' => 'admin', 'middleware' => AccessAuthenticateMiddleware::class], function ($api) {
                foreach (glob(base_path('routes/admin') . '/*.php') as $file) {
                    require $file;
                }
            });
        });
    }
    
    //微信第三方平台v1
    private function mapTppV1Routes() {
        Route::group([
            'prefix' => 'api/tpp/v1',
        ], function ($router) {
            foreach (glob(base_path('routes/tpp/v1') . '/*.php') as $file) {
                require $file;
            }
        });
    }
    
    /**
     * 开放应用管理接口
     */
    private function mapApplicationRoutes() {
        /** @var \Dingo\Api\Routing\Router $api */
        $api = app(\Dingo\Api\Routing\Router::class);
    
        // api接口
        $api->version(['v1'], function (\Dingo\Api\Routing\Router $api) {
            // 以下路由需求授权
            $api->group(
                [
                    'prefix' => 'application',
                    'middleware' => [
                        \App\Application\Middleware\ApplicationApiHttpAuthMiddleware::class,
                    ]
                ],
                function ($api) {
                    foreach (glob(base_path('routes/application') . '/*.php') as $file) {
                        require $file;
                    }
                }
            );
        });
    }
}
